<?php // defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
$url = $this->baseUrl(). '/application/modules/Ynadvsearch/externals/scripts/ynadvsearch.php'; 

?>
<style type="text/css">
form#global_search_form > ul{
 	position: absolute;
	top: 23px; 	
}
div.layout_core_menu_mini,
.layout_core_menu_mini #core_menu_mini_menu,
.layout_core_menu_mini > div > ul,
li#global_search_form_container {
    overflow: visible;
}
</style>
<script type="text/javascript">
var ynadvsearch = {
    is_space: false,
    input: null,
    ed: function () {
        Asset.javascript(en4.core.baseUrl + 'application/modules/Ynadvsearch/externals/scripts/ynadvsearch.js', {
            onLoad: ynadvsearch.eu
        });
    },
    cleanCss: function(e) {
        var p = e.parentNode;
        while (p && p.style != null) {
           // p.style.overflow = 'visible';
            p = p.parentNode;
        }
    },
    init: function () {
        var e = ynadvsearch.input = document.id('global_search_field');
        ynadvsearch.cleanCss(e);
        ynadvsearch.ed();
    }
};
ynadvsearch.eu = function(event){
	 var item_type='';
	 var maxre = <?php echo $this->maxRe; ?>;
	 $$(".tag-autosearch").each(function(e){
		    var doc = document.body;
		    doc.removeChild(e);
		});	 
		
	 new YnAdsAutocompleter.RequestAdSearch.JSON('global_search_field', '<?php echo $url// echo $this->url(array('module' => 'core', 'controller' => 'advsearch', 'action' => 'index',/*'maxre' => $this->maxRe*/), 'default', true) ?>', {
		  	'postVar': 'search',		  		       	        	        	        		  	
	        'className': 'tag-autosearch',
	        'cache': false,	        
	        'tokenFormat' : 'object',
	        'tokenValueKey' : 'label',
	        'maxChoices': 20,
	        'width': 300,
	        'postData' : {
				'maxre' : <?php echo $this->maxRe; ?>,
				'user_name':<?php echo $this->user_name; ?>,						
				'group_name':<?php echo $this->group_name; ?>,
				'group_description':<?php echo $this->group_description; ?>,				
				'page_name':<?php echo $this->page_name; ?>,
				'page_description':<?php echo $this->page_description; ?>,
				'directory_name':<?php echo $this->directory_name; ?>,
				'directory_description':<?php echo $this->directory_description; ?>,
				'other_name':<?php echo $this->other_name; ?>,
				'other_description':<?php echo $this->other_description; ?>,
				'static_base_url':'<?php echo Zend_Registry::get('StaticBaseUrl');?>'						
		    },
	        'injectChoice': function(token){
	        		// show group
	        		if(item_type !=token.type && token.type != 'see_more_link' && token.type != 'no_result_found_link'){
		        		item_type = token.type;
		        		var group_li = new Element('li', {'class': 'group-results','html': token.type_label}).inject(this.choices);
					}
					if (token.label == "No results were found") {
						var choice = new Element('li', {'class': 'autocompleter-choices-nophoto', 'id':'noresult'});
					}
					else {
		  				var choice = new Element('li', {'class': 'autocompleter-choices', 'id':token.label});
					}
		  			var choice_span = new Element('div', {'class': 'result-info'}).inject(choice);
		  			var choice_a = new Element('a', {
		            	'class': 'autocompleter-choices-a',
		            	'href':  token.url,
		            	'html': token.label
		            }).inject(choice_span);
		            // photo
		  			if(typeof(token.photo)){
		  				choice_a.set('html',token.photo);
		            }
		            //label
		            var choice_label = new Element('span',{'html': token.label}).inject(choice_a);		            
		  			if(typeof(token.type) && token.type=='see_more_link'){
	        			choice.addClass('see_more_link');
	        			new Element('span',{'class':'arrow'}).inject(choice_label);
	        			new Element('span',{
		        			'class':'ynserach_sub_text',
		        			'html': token.label2
			        	}).inject(choice)
		        	}
		        	// normal result
		  			else if (token.type !='no_result_found_link'){
			  			//category
			            var choice_category= new Element('span',{'html': token.type_label,'class':'type'}).inject(choice_a);
			  		}
		  			this.addChoiceEvents(choice).inject(this.choices);
	                choice.store('autocompleteChoice', token);
	  		}, //injectChoice
	  		onChoiceSelect: function(choice){	  	
				var a_tag = choice.getElements('a');
		  		var url = a_tag[0].getProperty('href');
		  		document.location = url;	
		  	},
		  	onCommand: function(e){
		  		
                if(e.key == 'space')
                {
                	 ynadvsearch.is_space = true;
                }
                else
                {
                    ynadvsearch.is_space = false;
                }
           },
		  	onRequest: function(){
		  		this.cached = [];
		  		var re = /\s*((\S+\s*)*)/;
				var trimquery = document.id('global_search_field').value;
				var l=0; var r=trimquery.length -1;
				while(l < trimquery.length && trimquery[l] == ' ')
				{	l++; }
				trimquery = trimquery.substring(l);
				if (trimquery && ynadvsearch.is_space == false)
				{
			  		document.id('global_search_field').setStyle('background', "url('application/modules/Core/externals/images/ajax-loader.gif') no-repeat right ");
				}
			},
			onComplete: function(){
				document.id('global_search_field').setStyle('background', '');
				<?php   echo ($this->align == 1)? "document.id('global_search_field').getNext().style.right = 0;document.id('global_search_field').getNext().style.top = '100%';" : '';?>;
				item_type = '';			
			}
	    });
};
window.addEvent('domready', ynadvsearch.init);
function catchEnter(e)
{
	if (!e) e = window.event; 
	var code = (e.keyCode) ? e.keyCode : e.which;
	if (code == 13 || code == 3) {
			var element = document.createElement("input");
			element.setAttribute("type", "hidden");
		    element.setAttribute("value", '1');
		    element.setAttribute("name", 'is_search');
		    document.id('global_search_form').appendChild(element);
			document.id('global_search_form').action = 'ynadvsearch/search';
	}
}
window.onload = function() { document.id('global_search_field').onkeypress = catchEnter; };
</script>